﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Configuration;

using TravelSite.DAL;
using TravelSite.DAL.Repositories;
using TravelSite.Code;

namespace TravelSite.Admin
{
    public partial class Admin : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SetActiveLink();
        }

        protected void lbLogout_Click(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();
            Session.Abandon();

            // clear authentication cookie
            HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, "");
            cookie1.Expires = DateTime.Now.AddYears(-1);
            Response.Cookies.Add(cookie1);

            // clear session cookie (recommend)
            HttpCookie cookie2 = new HttpCookie("ASP.NET_SessionId", "");
            cookie2.Expires = DateTime.Now.AddYears(-1);
            Response.Cookies.Add(cookie2);

            FormsAuthentication.RedirectToLoginPage();
        }

        private void SetActiveLink()
        {
            /*if (string.IsNullOrEmpty(tophlPackages.HRef))
            {
                using (var repo = RepositoryFactory.ResolveRepository<UserRepository>())
                {
                    TravelSite.Entities.User item = repo.Find((Guid)Membership.GetUser().ProviderUserKey);
                    tophlPackages.HRef = WebConfigurationManager.AppSettings["PackagesAdminUrl"] + "?authcode=" + item.AuthCode.ToString();
                }
            }*/

            string namePage = Page.GetType().FullName;

            tophlDashboard.Attributes["class"] = "dashboard_nav";
            tophlUserManagement.Attributes["class"] = "user_management_nav";
            tophlContactManagement.Attributes["class"] = "contacts_nav";
            tophlContracts.Attributes["class"] = "contracts_nav";
            tophlContractTemplate.Attributes["class"] = "templates_nav";
            tophlTravelPackage.Attributes["class"] = "package_nav";
            tophlVacationOption.Attributes["class"] = "options_nav";
            tophlReferences.Attributes["class"] = "reference_nav";
            tophlEmailSettings.Attributes["class"] = "email_settings_nav";
            tophlBookings.Attributes["class"] = "dashboard_nav";
        
            if (namePage == "ASP.admin_dashboard_aspx")
            {
                tophlDashboard.Attributes["class"] += " active active_dashboard";
            }
            else if (namePage == "ASP.admin_usermanagement_aspx")
            {
                tophlUserManagement.Attributes["class"] += " active active_user_management";
            }
            else if (namePage == "ASP.admin_contactmanagement_aspx")
            {
                tophlContactManagement.Attributes["class"] += " active active_contacts";
            }
            else if (namePage == "ASP.admin_contracts_aspx")
            {
                tophlContracts.Attributes["class"] += " active active_contracts";
            }
            else if (namePage == "ASP.admin_contracttemplate_aspx")
            {
                tophlContractTemplate.Attributes["class"] += " active active_templates";
            }
            else if (namePage == "ASP.admin_travelpackage_aspx")
            {
                tophlTravelPackage.Attributes["class"] += " active active_package";
            }
            else if (namePage == "ASP.admin_vacationoption_aspx")
            {
                tophlVacationOption.Attributes["class"] += " active active_options";
            }
            else if (namePage.Contains("references"))
            {
                tophlReferences.Attributes["class"] += " active active_reference";
            }
            else if (namePage == "ASP.admin_emailsetting_aspx")
            {
                tophlEmailSettings.Attributes["class"] += " active active_email_settings";
            }
            else if (namePage.Contains("bookings"))
            {
                tophlBookings.Attributes["class"] += " active active_dashboard";
            }
        }
    }
}